package com.shiva.oms.logview.api.view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.sf.shiva.oms.common.constant.LogParseConfigEnum;
import com.shiva.oms.biz.CustomLogParseCfgBiz;
import com.shiva.oms.logview.domain.ConnectCfg;
import com.shiva.oms.logview.domain.LogParseCfg;

/**
 * 
 * 
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月16日         LA.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author LA.01367604
 * @version 1.0
 */
public class PatternCfgGui extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private String[] headerNames = new String[] { "ID", "配置名", "日志类型", "内容格式", "日期格式", "文件编码" };
    private JTable table;
    private DefaultTableModel tableModel;
    private CustomLogParseCfgBiz customLogParseCfgBiz = CustomLogParseCfgBiz.getInstance();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            PatternCfgGui dialog = new PatternCfgGui();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PatternCfgGui() {
        this(null, null);
    }

    /**
     * Create the dialog.
     */
    public PatternCfgGui(JFrame parent, LogParseCfg logParseCfg) {
        super(parent, true);
        setTitle("日志模板配置管理");
        setBounds(100, 100, 738, 364);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JToolBar toolBar = new JToolBar();
            toolBar.setBounds(5, 5, 712, 23);
            contentPanel.add(toolBar);
            {
                JButton button = new JButton("新建");
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        NewConnectCfgGui dialog = new NewConnectCfgGui(PatternCfgGui.this);
                        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                        dialog.setVisible(true);
                        fresh();
                    }
                });
                toolBar.add(button);
            }
            {
                JButton button = new JButton("删除");
                button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int selectedRowIndex = table.getSelectedRow();
                        if (selectedRowIndex > 0) {
                            String id = (String) table.getValueAt(selectedRowIndex, 0);
                            customLogParseCfgBiz.removePattern(id);
                            tableModel.removeRow(selectedRowIndex);
                            table.revalidate();
                        }
                    }
                });
                {
                    JButton button_1 = new JButton("编辑");
                    button_1.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e) {}
                    });
                    toolBar.add(button_1);
                }
                toolBar.add(button);
            }
            {
                JButton button = new JButton("刷新");
                button.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        fresh();
                    }
                });
                toolBar.add(button);
            }
        }
        {
            // 初始化table值
            Object[][] datas = getArrayFromList(loadList());
            tableModel = new DefaultTableModel(datas, headerNames);
            table = new JTable(tableModel);
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setBounds(5, 28, 712, 260);
            contentPanel.add(scrollPane);
            // scrollPane.setViewportView(table);
        }
    }

    /**
     * 刷新table的显示
     * 
     * @author LA.01367604-2018年4月18日
     */
    private void fresh() {
        List<LogParseCfg> list = reloadList();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);// 清除原有行在
        tableModel.setDataVector(getArrayFromList(list), headerNames);
        table.revalidate();
    }

    private List<LogParseCfg> loadList(){
        List<LogParseCfg> list = new ArrayList<>();
        for (LogParseConfigEnum elem : LogParseConfigEnum.values()) {
            LogParseCfg cfg= new LogParseCfg(elem.getCfgName(),elem.getType(),elem.getPattern(),elem.getDateFormat(),elem.getCharset());
            list.add(cfg);
        }
        list.addAll(customLogParseCfgBiz.getList());
        return list;
    }

    private List<LogParseCfg> reloadList() {
        List<LogParseCfg> list = new ArrayList<>();
        for (LogParseConfigEnum elem : LogParseConfigEnum.values()) {
            LogParseCfg cfg = new LogParseCfg(elem.getCfgName(), elem.getType(), elem.getPattern(), elem.getDateFormat(), elem.getCharset());
            list.add(cfg);
        }
        list.addAll(customLogParseCfgBiz.reloadList());
        return list;
    }
    /**
     * list对象集合转数组表格数据
     * 
     * @param list
     * @return
     * @author LA.01367604-2018年4月17日
     */
    private Object[][] getArrayFromList(List<LogParseCfg> list) {
        int maxData = list.size();
        Object[][] data = new Object[maxData][headerNames.length];
        for (int i = 0; i < list.size(); i++) {
            LogParseCfg model = list.get(i);
            data[i][0] = model.getId();
            data[i][1] = model.getCfgName();
            data[i][2] = model.getType();
            data[i][3] = model.getPattern();
            data[i][4] = model.getDateFormat();
            data[i][5] = model.getCharset();
        }
        return data;
    }

    /**
     * 数组表格数据转list对象集合
     * 
     * @param data
     * @return
     * @author LA.01367604-2018年4月17日
     */
    private List<ConnectCfg> getListFromArray(Object[][] data) {
        List<ConnectCfg> list = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            ConnectCfg model = new ConnectCfg();
            model.setId((String) data[i][0]);
            model.setCfgName((String) data[i][1]);
            model.setIp((String) data[i][2]);
            model.setUser((String) data[i][3]);
            model.setPassword((String) data[i][4]);
            model.setFileDir((String) data[i][5]);
            list.add(model);
        }
        return list;
    }

}
